/// 获取TS类型
template getType(T) {
	import dast.gen.tsapi,
	jinji.rpc,
	std.traits;

	static if (is(T == Request))
		enum getType = "";
	else static if (is(T == enum)) {
		static if (__traits(isSame, __traits(parent, T), generated))
			enum getType = T.stringof;
		else
			enum getType = TSTypeOf!(OriginalType!T);
	} else
		enum getType = TSTypeOf!T;
}

int main() {
	debug (APIDoc) {
		import dast.gen.tsapi,
		jinji,
		jinji.rpc,
		std.traits,
		tame.io.file;

		File r = File("views/gen.ts", "wb");
		r.put(`// Auto generated by DAST
/**
 * 筛选器
 */
export type Filter = [
	/** 列名 */
	key: string,
	/** 筛选操作 */
	op: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -1,
	/** 筛选值 */
	value: string,
	/** 逻辑操作 */
	conj: 0 | 1 | 2
]
export type PostInfo = Omit<Post, 'id'>
`);
		alias m = ForModules!modules;
		ForModules!tables.genTypeDef!getType(r);
		// API函数名列表
		r.put("\nexport const funcs: (keyof Actions)[] = [");
		foreach (f; AllActions!(Action, modules)) {
			foreach (attr; getUDAs!(f, Action)) {
				r.put('\'');
				r.put(getName!(attr, __traits(identifier, f)));
				r.put("', ");
			}
		}
		r.put("]\n\nexport interface Actions {");
		m.genAPIDef!(Action, getType)(r);
		r.put("}\n");
		ForModules!generated.genEnum(r);
	} else version (App) {
		//extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
		import dast.config,
		dast.wsrpc,
		jinji.config,
		jinji.db,
		jinji.middleware,
		jinji.server,
		jinji.util.crypto,
		jinji.util.error;

		import jinji : modules;

		try {
			readcfg!C("config.yml");
			// 初始化密钥
			checkArgRange("tokenSecretKey",
				!C.tokenSecretKey.length || C.tokenSecretKey.length >= 10,
				"tokenSecretKey长度不能小于10");
			initKey(C.tokenSecretKey);
			connDB();
			initDFH();
			auto ws = new WSServer!modules;
			ws.use(&handle);
			ws.use(&getThumb);
			ws.use(&serve);
			ws.settings.listen = C.listen;
			ws.settings.maxConnections = C.maxConn;
			ws.settings.bufferSize = 32 << 10;
			wapp = typeof(wapp)("http://localhost" ~ C.listen, C.userDataDir);
			ws.run();
		} catch (Exception e) {
			fatal(e.toString());
		}
	}
	return 0;
}
